package org.chen.learn.demo.jgit;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * JGit运行上下文
 *
 * @author chensj
 * @date 2020-03-29 18:55
 */
public class JGitContext {

    /** logger */
    private static final Logger logger = LoggerFactory.getLogger(JGitContext.class);
    
    static List<String> modules = new ArrayList<>();

    static {
        modules.add("http://tfs2018-web.winning.com.cn:8080/tfs/WINNING-6.0/TMTS/_git/tmts_ccp_server");
        modules.add("http://tfs2018-web.winning.com.cn:8080/tfs/WINNING-6.0/TMTS/_git/tmts_cdp_ymer");
        modules.add("http://tfs2018-web.winning.com.cn:8080/tfs/WINNING-6.0/TMTS/_git/tmts_cop_comm");
        modules.add("http://tfs2018-web.winning.com.cn:8080/tfs/WINNING-6.0/TMTS/_git/tmts_cop_client");
    }

    /**
     * 获取文件检出文件夹
     *
     * @return 检出文件夹路径
     */
    public static String getWorkDir() {
        return "D:\\git_workspace";
    }

    /**
     * 用户名
     *
     * @return
     */
    public static String getUsername() {
        return "chensj";
    }

    /**
     * 密码
     *
     * @return
     */
    public static String getPassword() {
        return "Chenshijie1988";
    }

    /**
     * 获取秘钥信息
     *
     * @return 秘钥
     */
    public static UsernamePasswordCredentialsProvider getCredential() {
        return new UsernamePasswordCredentialsProvider(getUsername(), getPassword());
    }

    public static void main(String[] args) {
        try {
            for (String module : modules) {
                logger.info("开始checkout {}",module);
                String path = null;
                if (module.equals(".git")) {
                    path = module.substring(module.lastIndexOf("/") + 1, module.lastIndexOf("."));
                } else {
                    path = module.substring(module.lastIndexOf("/") + 1);
                }
                String targetPath = getWorkDir() + File.separator + path;
                File targetFile = new File(targetPath);
                if (targetFile.exists()) {
                    Git git = Git.open(new File(targetPath));
                    git.pull().setRemoteBranchName("master")
                            .setCredentialsProvider(getCredential())
                            .call();
                } else {
                    Git.cloneRepository()
                            .setURI(module)
                            .setCredentialsProvider(getCredential()).setBranch("master")
                            .setDirectory(new File(targetPath))
                            .call();
                }
            }
        } catch (GitAPIException | IOException e) {
            //e.printStackTrace();
            logger.info("git error {}",e.getMessage());
        }
    }


}
